require 'hpricot'
require 'net/http'
require "open-uri"

$KCODE='u'
require 'nkf'
require 'jcode'
require 'iconv'

def get(url)
  #Net::HTTP.get(URI.parse(url))
  html = ""
  file = File.open("html.txt")
  html = cn_str(file.readlines.join)
  
#  file.each do |line|
#    line = cn_str(line)    
#    html = html + line
#  end
  file.close
  html
end


def cn_str(str)
  case NKF.guess(str)
   when NKF::ASCII
     puts "ASCII"
   when NKF::JIS
     puts  "ISO-2022-JP"
   when NKF::SJIS
     puts  "Shift_JIS"
   when NKF::EUC
     puts "EUC-JP"
   when NKF::UTF8
     puts  "UTF-8"
   when NKF::UTF16
     puts "UTF-16"
   when NKF::UNKNOWN
    puts "UNKNOWN"
   when NKF::BINARY
     puts "BINARY"
   end   
   Iconv.conv('gbk', 'utf-8', str)   
end


def main
  url = "http://cache12.tianya.cn/publicforum/content/free/1/1139667.shtml"
  html = get(url)  
  
  puts html
  doc = Hpricot.parse(html)
  #puts doc
  

#  doc.search("table[@borderColor=#ffffff]/p").each do |p|
#    puts p.inner_html
#  end
  
  puts "OK!!!"
end


main

